package com.wgts.wgts.mvp.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerThridWriteInvitationCodeComponent;
import com.wgts.wgts.dagger.module.ThridWriteInvitationCodeModule;
import com.wgts.wgts.mvp.presenter.IThridWriteInvitationCodePresenter;
import com.wgts.wgts.mvp.view.IThridWriteInvitationCodeActivityView;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.netmodle.resp.LoginEntity;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ThridWriteInvitationCodeActivity extends BaseActivity implements IThridWriteInvitationCodeActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.edit_invitation_code)
    EditText editInvitationCode;
    @BindView(R.id.tv_ensure)
    TextView tvEnsure;
    @BindView(R.id.linear_ensur)
    FrameLayout linearEnsur;

    private String phone;

    private String smsCode;

    private String uid;

    private String unionid;


    @Inject
    IThridWriteInvitationCodePresenter presenter;

    @Override
    public void init() {
        phone = getIntent().getStringExtra("phone");
        smsCode = getIntent().getStringExtra("smsCode");
        uid = getIntent().getStringExtra("uid");
        unionid = getIntent().getStringExtra("unionid");

        DaggerThridWriteInvitationCodeComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .thridWriteInvitationCodeModule(new ThridWriteInvitationCodeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.send_invitation_code);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tvPhone.setText(phone);
    }

    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
        dialogDismiss();
    }

    @OnClick(R.id.linear_ensur)
    public void onViewClicked() {
        showDialog();
        presenter.bindUser(editInvitationCode.getText().toString(), uid, smsCode, phone, unionid);
    }

    @Override
    public void loginResult(LoginEntity loginEntity) {
        dialogDismiss();
        AppInfor.getInstance().setAccessToken(loginEntity.accessToken);
        AppInfor.getInstance().setRefreshToken(loginEntity.refreshToken);
        AppInfor.getInstance().setUserState(loginEntity.userRole);
        AppInfor.getInstance().setUserYQCode(loginEntity.userCode);
        AppInfor.getInstance().setInviteCode(loginEntity.userCode);
        Intent intent = new Intent(ThridWriteInvitationCodeActivity.this, MoHuMainActivity.class);
        startActivity(intent);
    }
}
